package com.box.modules.report.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.box.common.utils.PageUtils;
import com.box.common.utils.Query;
import com.box.modules.report.dao.BoxRowProcessedDao;
import com.box.modules.report.entity.BoxRowDataEntity;
import com.box.modules.report.entity.BoxRowProcessedEntity;
import com.box.modules.report.service.BoxRowProcessedService;
import com.box.modules.report.vo.ReportDetailVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("boxRowProcessedService")
public class BoxRowProcessedServiceImpl extends ServiceImpl<BoxRowProcessedDao, BoxRowProcessedEntity> implements BoxRowProcessedService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BoxRowProcessedEntity> page = this.page(
                new Query<BoxRowProcessedEntity>().getPage(params),
                new QueryWrapper<BoxRowProcessedEntity>()
        );

        return new PageUtils(page);
    }

    /*
     * 解析一条原生数据
     * 1.去除重复时间段
     * */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Boolean addRowProcessed(BoxRowDataEntity boxRowDataEntity) {
        //已经存入的时间段
        LambdaQueryWrapper<BoxRowProcessedEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BoxRowProcessedEntity::getBoxNo, boxRowDataEntity.getBoxNo()).eq(BoxRowProcessedEntity::getReportTime, boxRowDataEntity.getReportTime());
        List<BoxRowProcessedEntity> dbList = this.list(queryWrapper);

        //插入其他时间段数据(除去已存入的时间段)
        List<ReportDetailVO> detailVOList = JSONArray.parseArray(boxRowDataEntity.getList(), ReportDetailVO.class);

        List<BoxRowProcessedEntity> boxRowProcessedEntityList = new ArrayList<>();
        for (ReportDetailVO one : detailVOList) {
            //判断是否已存入
            Boolean isSaved = false;
            for (BoxRowProcessedEntity dbOne : dbList) {
                if (one.getPeriod() == dbOne.getPeriod()) {
                    isSaved = true;
                }
            }
            //如果没有存入，插入数据库
            if (!isSaved) {
                BoxRowProcessedEntity boxRowProcessedEntity = new BoxRowProcessedEntity();
                boxRowProcessedEntity.setMessageId(boxRowDataEntity.getMessageId());
                boxRowProcessedEntity.setBoxNo(boxRowDataEntity.getBoxNo());
                boxRowProcessedEntity.setReportTime(boxRowDataEntity.getReportTime());
                boxRowProcessedEntity.setPeriod(one.getPeriod());
                boxRowProcessedEntity.setData(one.getData());
                boxRowProcessedEntity.setUpTime(one.getUpTime());
                boxRowProcessedEntityList.add(boxRowProcessedEntity);
            }
        }
        this.saveBatch(boxRowProcessedEntityList);
        return true;
    }

}
